var app = getApp();

Page({
    data: {
        denglu:false,
        text: "迈旗出品",
        userInfo: {},
        cardnum: "0",
        booknum: "0",
        bg_color: "#be69ff",
        copyright: "",
        phone: "",
        input: {
            isHide: !1,
            uname: "",
            uphone: ""
        },
        uname: "",
        uphone: "",
        uid: "",
        toast: {
            isHide: !1,
            content: ""
        },
        thispath: ""
    },
    onLoad: function(t) {
        var a = this;
        a.setData({
            thispath: a.route
        }), a.personal(), a.GetuserInfo();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    personal: function(t) {
        var a = this;
        wx.login({
            success: function(t) {
                app.util.request({
                    url: "entry/wxapp/personal",
                    data: {
                        code: t.code
                    },
                    cache: "0",
                    success: function(t) {
                        console.log(t), a.setData({
                            booknum: t.data.booknum,
                            cardnum: t.data.cardnum,
                            uid: t.data.uid,
                            copyright: t.data.copyright,
                            phone: t.data.phone,
                            uname: t.data.uName,
                            uphone: t.data.uPhone,
                            "input.isHide": 1 == t.data.ishide,
                            "input.uname": t.data.uName,
                            "input.uphone": t.data.uPhone
                        }), setTimeout(function() {
                            wx.setNavigationBarColor({
                                frontColor: "#ffffff",
                                backgroundColor: "#fda78e",
                                navigationBarTextStyle: "white",
                                animation: {
                                    duration: 400,
                                    timingFunc: "easeIn"
                                }
                            }), wx.setNavigationBarTitle({
                                title: t.data.title
                            });
                        }, 100);
                    }
                });
            }
        });
    },
    GetuserInfo: function(t) {
        // var a = this;
        // app.util.getUserInfo(function(t) {
        //     a.setData({
        //         userInfo: t.wxInfo
        //     });
        // });

      console.log("获取用户信息");
      var n = this;

      var t = wx.getStorageSync("userInfo");
      if (t && t.wxInfo != "") {

        this.setData({
          userInfo: t.wxInfo
        });

      } else {
        this.setData({ denglu: true });
      }
    },
    getuserinfo_zhz: function (e) {
      var that = this;
      console.log(e);
      if (e.detail.errMsg.indexOf("fail") == -1) {

        var userInfo = e.detail.userInfo;
        //重新登录
        that.setData({ denglu: false });

        console.log("start login");
        var user = {
          sessionid: "",//login 获取code 时回来的值
          wxInfo: "",//getuserinfo 确认后回来的值
          memberInfo: ""
        };

        user.wxInfo = userInfo;

        wx.login({
          success: function (n) {
          app.util.request({
              url: "auth/session/openid",
              data: {
                code: n.code
              },
              cachetime: 0,
              success: function (n) {
                if (!n.data.errno) {
                  user.sessionid = n.data.data.sessionid;
                  wx.setStorageSync("userInfo", user);

                  app.util.request({
                    url: "auth/session/userinfo",
                    data: {
                      signature: e.detail.signature,
                      rawData: e.detail.rawData,
                      iv: e.detail.iv,
                      encryptedData: e.detail.encryptedData
                    },
                    method: "POST",
                    header: {
                      "content-type": "application/x-www-form-urlencoded"
                    },
                    cachetime: 0,
                    success: function (n) {
                      n.data.errno || (user.memberInfo = n.data.data, wx.setStorageSync("userInfo", user));
                      that.setData({
                        userInfo: user.wxInfo
                      });
                    }, fail: function (n) {
                      console.log("后台同步登录失败");
                    }
                  })
                }
                //|| (

                //=====================================
                //  wx.getUserInfo({
                //   success: function (n) {
                //  t.wxInfo = n.userInfo,

                //   },
                //   fail: function () {
                //     "function" == typeof e && e(t)
                //   },
                //   complete: function () { }
                // })
                //=================

                //)
              }
            })
          }
          // ,
          // fail: function () {
          //   wx.showModal({
          //     title: "获取信息失败",
          //     content: "请允许授权以便为您提供给服务",
          //     success: function (e) {
          //       e.confirm && util.getUserInfo()
          //     }
          //   })
          // }
        })







      }
      else {
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝授权,请务必授权。',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.openSetting({
                success: (res) => {
                  if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                    //  wx.showModal({
                    //   title: '登录',
                    //   content: '您点击了拒绝授权,无法进入个人中心,点击确定重新获取授权。',
                    //   showCancel: false});



                  } else {

                  }
                },
                fail: function (res) {
                }
              })
            }
          }
        })
      }


    },

    close: function(t) {
        this.setData({
            "input.isHide": !1
        });
    },
    bind: function(t) {
        this.setData({
            "input.isHide": !0
        });
    },
    uname: function(t) {
        this.setData({
            "input.uname": t.detail.value
        });
    },
    uphone: function(t) {
        return this.show("请点击右侧小图标获取"), !1;
    },
    save: function(t) {
        var a = this, n = a.data.uid, e = a.data.input.uname, o = a.data.input.uphone, i = a.data.userInfo.nickName;
        return "" == n ? (a.show("请输入姓名"), !1) : "" == e ? (a.show("请输入姓名"), !1) : "" == o ? (a.show("请输入联系电话"), 
        !1) : /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|18[0-9]{9}$|17[0-9]{9}$/.test(o) ? void app.util.request({
            url: "entry/wxapp/memberbind",
            data: {
                uname: e,
                uphone: o,
                uid: n,
                nickname: i
            },
            cache: "0",
            success: function(t) {
                "1" == t.data.status && a.setData({
                    "input.isHide": !1
                }), a.show(t.data.msg), console.log(t);
            }
        }) : (a.show("联系电话错误"), !1);
    },
    show: function(t) {
        var a = this;
        this.setData({
            "toast.isHide": !0,
            "toast.content": t
        }), setTimeout(function() {
            a.setData({
                "toast.isHide": !1
            });
        }, 2e3);
    },
    tel: function(t) {
        wx.makePhoneCall({
            phoneNumber: this.data.phone
        });
    },
    goindex: function() {
        wx.navigateTo({
            url: "/mii_ang/pages/index/index"
        });
    },
    onShareAppMessage: function(t) {
        var a = this;
        return "button" === t.from && console.log(t.target), {
            title: "个人中心",
            path: "/" + a.data.thispath,
            success: function(t) {
                a.show("感谢分享");
            },
            fail: function(t) {
                a.show("分享取消");
            }
        };
    },
    getPhoneNumber: function(a) {
        var n = this;
        wx.login({
            success: function(t) {
                console.log(a), "getPhoneNumber:ok" == a.detail.errMsg ? app.util.request({
                    url: "entry/wxapp/decode",
                    data: {
                        iv: a.detail.iv,
                        data: a.detail.encryptedData,
                        code: t.code
                    },
                    cachetime: "0",
                    success: function(t) {
                        console.log(t), "1" == t.data.status ? n.setData({
                            "input.uphone": t.data.phoneNumber
                        }) : n.show(t.data.msg);
                    }
                }) : n.show("获取失败,请允许系统获取您的手机号码");
            }
        });
    }
});